package service

import (
	"context"
	"fmt"
	"github.com/tencentyun/cos-go-sdk-v5"
	"mime/multipart"
)

type IFileService interface {
	UploadFile(client *cos.Client, filename string, file multipart.File) (string, error)
}
type FileService struct{}

func NewFileService() *FileService {
	return &FileService{}
}

func (f FileService) UploadFile(client *cos.Client, filename string, file multipart.File) (string, error) {

	_, err := client.Object.Put(context.Background(), filename, file, nil)
	if err != nil {
		return "", fmt.Errorf("failed to upload file to COS: %v", err)
	}

	// 拼接文件的访问 URL
	fileURL := client.BaseURL.BucketURL.String() + "/" + filename
	return fileURL, nil

}
